package com.linzhaowei.headhuntingservice.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.linzhaowei.headhuntingservice.ApplicantUpdatePersonal;
import com.linzhaowei.headhuntingservice.Delivered;
import com.linzhaowei.headhuntingservice.JobObjectiveManage;
import com.linzhaowei.headhuntingservice.R;
import com.linzhaowei.headhuntingservice.Start;
import com.linzhaowei.headhuntingservice.bean.User;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;
import java.util.Objects;

public class ApplicantPersonalFragment extends Fragment implements View.OnClickListener {
    private TextView name;
    private Button updatePersonal;
    private Button jobObject;
    private Button delivered;
    private Button exit;
    private String setName;
    private final static int SELECT_APPLICANT = 1;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_applicant_personal, container, false);
        initViews();
        setEvents();

        selectApplicant();

        return view;
    }


    private void initViews() {
        name = view.findViewById(R.id.a_p_name);
        updatePersonal = view.findViewById(R.id.a_p_update_personal);
        jobObject = view.findViewById(R.id.a_p_job_objective);
        exit = view.findViewById(R.id.a_p_exit);
        delivered=view.findViewById(R.id.a_p_delivered);
    }

    private void setEvents() {
        updatePersonal.setOnClickListener(this);
        jobObject.setOnClickListener(this);
        exit.setOnClickListener(this);
        delivered.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.a_p_update_personal:
                Intent intent = new Intent(getActivity(), ApplicantUpdatePersonal.class);
                startActivity(intent);
                break;


            case R.id.a_p_job_objective:
                Intent intent2 = new Intent(getActivity(), JobObjectiveManage.class);
                startActivity(intent2);
                break;

            case R.id.a_p_exit:
                Intent intent3 = new Intent(getActivity(), Start.class);
                startActivity(intent3);
                EMClient.getInstance().logout(true);
                Objects.requireNonNull(getActivity()).finish();
                break;

            case R.id.a_p_delivered:
                Intent intent4=new Intent(getActivity(), Delivered.class);
                startActivity(intent4);
                break;

        }
    }

    private void selectApplicant() {
        User user = new User();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        user.setId(sharedPreferences.getInt("id", -1));
        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();
                try {
                    String result = httpUtils.get(Ip.ip + "/user/select_applicant?id=" + user.getId());
                    JSONObject jsonObject = JSON.parseObject(result);
                    setName = jsonObject.getString("name");
                    Message message = handler.obtainMessage();
                    message.what = SELECT_APPLICANT;
                    handler.sendMessage(message);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }


    /**
     * 异步消息处理机制
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SELECT_APPLICANT:
                    name.setText(setName);
                    break;
                default:
                    break;
            }
        }
    };


}
